package com.fc.advbox.job;

import com.fc.advbox.entity.InvestorAcctEntity;
import com.fc.advbox.entity.InvestorAssetEntity;
import com.fc.advbox.service.IInvestorAcctService;
import com.fc.advbox.service.IInvestorAssetService;
import com.fc.advbox.utils.Constant;
import com.fc.advbox.utils.DateTimeHelper;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@Configurable
@EnableScheduling
public class InvestorMonthlySettleTask {
    private static final Logger log = LoggerFactory.getLogger(InvestorMonthlySettleTask.class);

    @Autowired
    private IInvestorAcctService investorAcctService;

    @Autowired
    private IInvestorAssetService iInvestorAssetService;

    //每月1号早晨1点8分执行一次
    @Scheduled(cron = "0 8 1 1 * *")
    public void runJobByCron() {
        log.info("Scheduling Tasks InvestorMonthlySettleTask : start at: " + new Date());
        log.info("Starting ...");
        interestSettle();
        log.info("completed");
        log.info("Tasks InvestorMonthlySettleTask: complete at: " + new Date());
    }

    /**
     * 每月结息
     */
    private void interestSettle(){
        int recTotal = investorAcctService.queryTotal(new HashedMap());
        for(int i = 0; i <= recTotal/100; i++){
            List<InvestorAcctEntity> acctList = investorAcctService.queryLimit(i*100,100);
            for(InvestorAcctEntity acct: acctList){
                Map<String, Object> param = new HashedMap();
                param.put("userId", acct.getUserId());
                List<InvestorAssetEntity> assetList = iInvestorAssetService.queryList(param);
                log.info("Start settle for UserId:["+acct.getUserId()+"]acct number:["+acct.getAcctNum()+"] asset number:"+assetList.size());
                settleInvestorAsset(assetList); //update profit
                log.info("End settle for UserId:["+acct.getUserId()+"] acct number:["+acct.getAcctNum()+"] asset number:"+assetList.size());
            }
        }
    }

    private void settleInvestorAsset(List<InvestorAssetEntity> assetList){
        Long userId = 0L;
        double interestTotal = 0;
        for(InvestorAssetEntity asset: assetList){
            if(asset.getAmount().doubleValue() == 0){
                continue;
            }
            if(asset.getStatus().equals(Constant.AssetStatus.INACTIVE)){
                continue;
            }
            if(asset.getInterestPerDay() == null){
                log.warn("Asset Id:["+asset.getId()+"] interest per day is empty. Please check" );
                continue;
            }
            if(asset.getInterestPerMonth() == null){
                log.warn("Asset Id:["+asset.getId()+"] interest per month is empty. Please check" );
                continue;
            }

            userId = asset.getUserId();
            double interest;
            int diff = DateTimeHelper.calculateDateDiff(asset.getBuyTime(),new Date());
            if(diff <30 ){
                interest = asset.getAmount().doubleValue() * asset.getInterestPerDay().doubleValue()*diff;
            }else{
                interest = asset.getAmount().doubleValue() * asset.getInterestPerMonth().doubleValue();
            }
            interestTotal += interest;
            log.info("Asset Id:["+asset.getId()+"] interest:["+interest+"]");
        }
        log.info("Interest total:["+interestTotal+"]");
        investorAcctService.increase(userId,interestTotal);
    }
}